package com.czh.zhiwen.common;

import com.czh.zhiwen.exception.BusinessException;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.*;
import io.reactivex.Flowable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Component
public class AiRequest {

    @Resource
    private ClientV4 client;

    private final Float TEMPERATURE = 0.95f;
    /**
     * 同步请求
     * @param messages
     * @return
     */

    public String doAiRequest(List<ChatMessage> messages){
        return doAiRequest(messages,false,TEMPERATURE);
    }
    /**
     * 通用请求(稳定)
     * @param messages
     * @param stream
     * @return
     */
    public String doAiRequest(List<ChatMessage> messages,Boolean stream){
        return doAiRequest(messages,stream,TEMPERATURE);
    }

    /**
     * 通用请求
     * @param systemChatMeg
     * @param userChatMeg
     * @return
     */
    public String doAiRequest(String systemChatMeg,String userChatMeg){

        List<ChatMessage> list = new ArrayList<>();
        ChatMessage systemChatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(),systemChatMeg );
        list.add(systemChatMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userChatMeg);
        list.add(userChatMessage);
        return doAiRequest(list,false,TEMPERATURE);
    }
    /**
     * 通用流式请求
     * @param systemChatMeg
     * @param userChatMeg
     * @return
     */
    public Flowable<ModelData> doStreamAiRequest(String systemChatMeg,String userChatMeg){
        List<ChatMessage> list = new ArrayList<>();
        ChatMessage systemChatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(),systemChatMeg );
        list.add(systemChatMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userChatMeg);
        list.add(userChatMessage);
        return doStreamAiRequest(list);
    }
    /**
     * 通用请求
     * @param userChatMeg
     * @param systemChatMeg
     * @param stream
     * @param temperature
     * @return
     */
    public String doAiRequest(String systemChatMeg,String userChatMeg,Boolean stream,Float temperature){

        List<ChatMessage> list = new ArrayList<>();
        ChatMessage systemChatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(),systemChatMeg );
        list.add(systemChatMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userChatMeg);
        list.add(userChatMessage);
        return doAiRequest(list,stream,temperature);
    }
    /**
     * 通用请求
     * @param messages
     * @param stream
     * @param temperature
     * @return
     */
    public String doAiRequest(List<ChatMessage> messages,Boolean stream,Float temperature){
        //构建请求
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                // 模型选择
                .model(Constants.ModelChatGLM4)
                // 不开启流式输出
                .stream(stream)
                // 随机度
                .temperature(temperature)
                // 执行的方式
                .invokeMethod(Constants.invokeMethod)
                // 请求消息
                .messages(messages)
                .build();
        //调用模型接口
        try{
            ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getData().getChoices().get(0).toString();
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 通用流式请求
     * @param messages
     * @return
     */
    public Flowable<ModelData> doStreamAiRequest(List<ChatMessage> messages ){
        //构建请求
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                // 模型选择
                .model(Constants.ModelChatGLM4)
                // 开启流式输出
                .stream(Boolean.TRUE)
                // 随机度
                .temperature(TEMPERATURE)
                // 执行的方式
                .invokeMethod(Constants.invokeMethod)
                // 请求消息
                .messages(messages)
                .build();
        //调用模型接口
        try{
            ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getFlowable();
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }

    }
}
